import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';

class DatePicker extends StatefulWidget {
  const DatePicker({Key? key}) : super(key: key);

  @override
  _DatePickerState createState() => _DatePickerState();
}

class _DatePickerState extends State<DatePicker> {

  var defaultDate="选择日期";
  var defaultTime="00：00";

  _showDatePicker(Function callback) {
    showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2000),
      lastDate: DateTime(2100),
      locale: const Locale('zh'),
    ).then((value) {
      callback(formatDate(value!, [yyyy, '-', mm, '-', dd,]));
      print(value);
    });
  }
  //选择时间
  _showTimePicker(Function callback) {
    showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    ).then((value) {
      callback(formatDate(DateTime.now(), [HH, ':', nn, ':', ss]));
      print(value);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("DatePicker"),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () {
                _showDatePicker((res){
                  print(res);
                  defaultDate=res;
                  setState(() {

                  });
                });
              },
              child: Text("$defaultDate"),
            ),
            TextButton(
              onPressed: () {
                _showTimePicker((res){
                  print(res);
                  defaultTime=res;
                  setState(() {

                  });
                });
              },
              child: Text("$defaultTime"),
            )
          ],
        ),
      ),
    );
  }
}
